This introductory course covers the basic aspects of the Python language, which you will need to get started in the area of Data Science. This course is dedicated mainly for those who already have experience with code or even another language such as R, Julia, Matlab, among others.
We will first start with Python syntax, as well as variable assignment and arithmetic operations.
Generally in a data science project, we need to present results of models, graphs and/or analysis. In this way, a first message that we could develop would be that our project was successful, for this we must define the variable where we will save the message "The project was successful."
message="The project was successful."
print(message)
The project was successful.
As you can see, exposing results in Python is not as difficult as we might think.
Just as in the previous code block, in Python you can work with different types of variables: Numeric, String, among other subtypes. But it should be noted that when we talk about numerical variables, with them we can perform all types of arithmetic operations and with the String type variables we can generate texts that show the output of a successful process or even generate documentation.
Below we can see an example of a numeric variable that stores the value 33 and a string variable that stores the text "This is my age"
number=33
phrase="This is my age"
However, we could work with both variables together and display it as an output, as shown below with the variable print()
print(phrase,number)
This is my age 33
Ya hemos visto dos tipos de variables,sin embargo si tenemos dudas acerca de la definción podemos verificar el tipo de variable con que estamos trabajando.
type(number)
int
type(phrase)
str
As we can see in the output of the block, the variable that stores the number value is of type "int", this is an abbreviation for "integer", instead for the phrase variable the type is "str" referring to "string" or text string. However, we could also define a variable that stores a numerical value as text, in this case we could not perform arithmetic operations with it.
other_number="28.2"
type(other_number)
print("The 'other_number' variable,stores the value",other_number)
The 'other_number' variable,stores the value 28.2
However, if we wanted to work with numerical variables with decimal values such as "30.2" "23.4" or others, we can define variable of type float.
number_float=30.2
print("The number_float variable,have the value",number_float,"and his type is",type(number_float))
The number_float variable,have the value 30.2 and his type is <class 'float'>
In this way, if we wanted to perform arithmetic operations with variables of type integer or float, we can perform the operations listed in the following table.
| Syntax | Operation | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| x + y | Addition | The sum of x and y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x - y | Subtraction | The difference between x and y y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x * y | Multiplication | The product between x and y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x / y | Division | The quotient between x e y. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x % y | Module | The remaining integer value, after dividing x and y. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x ** y | Empowerment | The result of raising x to y. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -x | Denial | The negative value of x. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operation | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| x == y | x is equal to y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x < y | x is minor than y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x <= y | x is minor or equal than y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x != y | x is not equal to y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x > y | x is greater than y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| x >= y | x is greater or equal to y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Method | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| capitalize() | Convert in capitalize the first character in the string. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| index() | Search the position of a character and return. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| split() | Separate a string and convert in a list. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| upper() | Convert in capitalize all character of a string. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lower() | Convert all character of a string in lower. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Attribute | Method | Detail | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Dimension Number | a.ndim() | Returns the number of dimensions of the array a. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Dimensions | a.shape() | Returns a tuple with the dimensions of the array a. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Size | a.size() | Returns the number of elements in array a. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data type | a.dtype() | Returns the data type of the elements of the array a. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Method | Operation | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| dot(b) | Scalar product | Determines the scalar product between vectors a and b. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| norm(a) | Module of a vector | Determines the module of the vector v. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a.dot(b) | Product of 2 matrices | Determines the matrix product of matrices a and b. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a.T | Transposed matrix | Determines the transposed matrix of the matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| a.trace() | Trace of a matrix | Determines the main diagonal sum of the square matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| det(a) | Determinant of a matrix | Returns the determinant of the matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inv(a) | Inverse Matrix | Determines the inverse matrix of the square matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eigvals(a) | Eigenvalues of a matrix | Determines the eigenvalues of the square matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eig(a) | Eigenvectors of a matrix | Determines the eigenvectors of the square matrix a. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| solve(a,b) | Solution of a system of equations | Determines the solution of a system of linear equations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Country | Medals | Quantity | |||
|---|---|---|---|---|---|
| South Korea | Gold | 25 | |||
| China | Gold | 10 | |||
| Canada | Gold | 9 | |||
| South Korea | Silver | 13 | |||
| China | Silver | 15 | |||
| Canada | Silver | 12 | |||
| South Korea | Bronze | 11 | |||
| China | Bronze | 8 | |||
| Canada | Bronze | 12 | |||